/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.common.util.encrypt.Md5
 * <p>
 * 类产生时间: 2014-12-19 23:37
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 项目组 : 苏州公安移动门户
 * <p>
 * 部门: 手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.nos.common.util.encrypt;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5工具类
 *
 * @author chenw
 * @time 2014-12-19 23:37
 */
public class Md5 {
    private static final char[] DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public Md5() {
    }

    public static String getMD5Info(String digestInfo) throws Exception {
        byte[] inputData = digestInfo.getBytes();
        byte[] bytes = encryptMD5(inputData);
        String md5Str = new String(encodeHex(bytes));
        return md5Str;
    }

    public static byte[] encryptMD5(byte[] data) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(data);
        return md5.digest();
    }

    public static char[] encodeHex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];
        int i = 0;
        for (int j = 0; i < l; ++i) {
            out[j++] = DIGITS[(240 & data[i]) >>> 4];
            out[j++] = DIGITS[15 & data[i]];
        }
        return out;
    }

    public static String getMD5Str(String str) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException var5) {
            System.out.println("NoSuchAlgorithmException caught!");
            System.exit(-1);
        } catch (UnsupportedEncodingException var6) {
            var6.printStackTrace();
        }
        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (byte aByteArray : byteArray) {
            if (Integer.toHexString(255 & aByteArray).length() == 1) {
                md5StrBuff.append("0").append(Integer.toHexString(255 & aByteArray));
            } else {
                md5StrBuff.append(Integer.toHexString(255 & aByteArray));
            }
        }
        return md5StrBuff.toString();
    }

    public static void main(String[] args) {
        System.out.println(getMD5Str("FDSFDSFLK245445LKJ4323dfkjs"));
    }
}
